package com.useeinfo.asg.open.system;

import com.useeinfo.asg.common.utils.CommUtil;
import com.useeinfo.asg.modules.security.biz.SystemAuthorityBiz;
import com.useeinfo.asg.modules.security.biz.SystemRoleBiz;
import com.useeinfo.asg.modules.security.entity.SystemAuthority;
import com.useeinfo.asg.modules.security.entity.SystemRole;
import com.useeinfo.framework.sugar.data.QueryParam;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * Created by 宝玉 on 2017/8/23.
 */
@Controller
@RequestMapping("/admin/system/authority")
public class SystemAuthorityAdminManageAction {

    @Autowired
    private SystemAuthorityBiz systemAuthorityBiz;

    @RequestMapping("/list")
    public String list(HttpServletRequest request, ModelMap mv) {

        return "/system/systemAuthority/list";
    }

    @RequestMapping("/listJson")
    @ResponseBody
    public JSONObject listJosn(@RequestParam(value = "page", required = false) String pageNowParam,
                               @RequestParam(value = "rows", required = false) String pageSizeParam,
                               @RequestParam(value = "sidx", required = false) String sidxParam,
                               @RequestParam(value = "sord", required = false) String sordParam) {

        QueryParam queryParam = new QueryParam(pageNowParam, pageSizeParam);

        return systemAuthorityBiz.findJSONList(queryParam);
    }

    @RequestMapping("/editPage")
    public String editPage(HttpServletRequest request, ModelMap mv) {

        long id = CommUtil.null2Long(request.getParameter("id"));
        if(id > 0) {
            SystemAuthority authority = systemAuthorityBiz.findModel(id);
            mv.put("authority", authority);
        }
        return "/system/systemAuthority/edit";
    }

    @RequestMapping("/addOrEdit")
    @ResponseBody
    public JSONObject addOrEdit(HttpServletRequest request) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        String authorityName = CommUtil.null2String(request.getParameter("authorityName"));
        String authorityDescription = CommUtil.null2String(request.getParameter("authorityDescription"));
        SystemAuthority authority = null;
        if(id < 0 || (authority = systemAuthorityBiz.findModel(id)) == null) {
            authority = new SystemAuthority();
            authority.setCreateTime(new Date());
        }
        authority.setAuthorityName(authorityName);
        authority.setAuthorityDescription(authorityDescription);

        //判断权限名称是否重复
        if (systemAuthorityBiz.existAuthorityName(authority)) {
            retJson.put("msg", "您输入的权限名称已存在，请重新输入");
            return retJson;
        }
        systemAuthorityBiz.addOrUpdate(authority);
        retJson = CommUtil.jsonResult(true);

        return retJson;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public JSONObject delete(HttpServletRequest request, ModelMap mv) {

        JSONObject retJson = CommUtil.jsonResult(false);
        long id = CommUtil.null2Long(request.getParameter("id"));
        SystemAuthority authority = null;
        if(id < 0 || (authority = systemAuthorityBiz.findModel(id)) == null) {
            retJson.put("msg", "未找到数据源");
            return retJson;
        }
        authority.setDeleteStatus(true);
        systemAuthorityBiz.update(authority);
        return retJson;
    }
}
